function _slicedToArray(arr, i) {
  return (
    _arrayWithHoles(arr) ||
    _iterableToArrayLimit(arr, i) ||
    _unsupportedIterableToArray(arr, i) ||
    _nonIterableRest()
  );
}
function _nonIterableRest() {
  throw new TypeError(
    'Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}
function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === 'string') return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === 'Object' && o.constructor) n = o.constructor.name;
  if (n === 'Map' || n === 'Set') return Array.from(o);
  if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
    return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  return arr2;
}
function _iterableToArrayLimit(arr, i) {
  var _i =
    null == arr
      ? null
      : ('undefined' != typeof Symbol && arr[Symbol.iterator]) || arr['@@iterator'];
  if (null != _i) {
    var _s,
      _e,
      _x,
      _r,
      _arr = [],
      _n = !0,
      _d = !1;
    try {
      if (((_x = (_i = _i.call(arr)).next), 0 === i)) {
        if (Object(_i) !== _i) return;
        _n = !1;
      } else
        for (
          ;
          !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i);
          _n = !0
        );
    } catch (err) {
      (_d = !0), (_e = err);
    } finally {
      try {
        if (!_n && null != _i.return && ((_r = _i.return()), Object(_r) !== _r)) return;
      } finally {
        if (_d) throw _e;
      }
    }
    return _arr;
  }
}
function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}
import { ScanOutlined } from '@ant-design/icons';
import { Spin } from 'antd';
import React, { useEffect, useMemo, useState } from 'react';
import './index.less';

/**
 * 钉钉扫码登录
 * @param param0
 * @returns
 */
export default function DingQrCode(_ref) {
  var appId = _ref.appId,
    redirectUrl = _ref.redirectUrl,
    pure = _ref.pure,
    iframeStyle = _ref.iframeStyle,
    onGetCode = _ref.onGetCode;
  var searchParams = new URLSearchParams(location.search);
  /** 钉钉回调 code */
  var code = searchParams.get('code');
  var _useState = useState(new Date().getTime()),
    _useState2 = _slicedToArray(_useState, 2),
    timestamp = _useState2[0],
    setTimestamp = _useState2[1];
  useEffect(function () {
    if (code) onGetCode(code);
  }, []);
  useEffect(
    function () {
      var handleLogin = function handleLogin(event) {
        if (event.origin === 'https://login.dingtalk.com') {
          var loginTmpCode = event === null || event === void 0 ? void 0 : event.data;
          if (loginTmpCode) {
            window.location.href = 'https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid='
              .concat(appId, '&response_type=code&scope=snsapi_login&state=')
              .concat(new Date().getTime(), '&redirect_uri=')
              .concat(encodeURIComponent(redirectUrl), '&loginTmpCode=')
              .concat(loginTmpCode);
          } else {
            setTimestamp(new Date().getTime());
          }
        }
      };
      window.addEventListener('message', handleLogin);
      return function () {
        window.removeEventListener('message', handleLogin);
      };
    },
    [appId, redirectUrl],
  );
  var iframeSrc = useMemo(
    function () {
      var params = new URLSearchParams();
      var goto = 'https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid='
        .concat(appId, '&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=')
        .concat(encodeURIComponent(redirectUrl));
      params.set('goto', goto);
      params.set('style', 'border:none;background-color:#FFFFFF;');

      // https://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js 实际内容
      return 'https://login.dingtalk.com/login/qrcode.htm?' + params.toString();
    },
    [appId, redirectUrl, timestamp],
  );
  return code
    ? /*#__PURE__*/ React.createElement(
        'div',
        {
          style: {
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center',
            height: '100vh',
          },
        },
        /*#__PURE__*/ React.createElement(Spin, {
          tip: '\u9489\u9489\u767B\u5F55\u4E2D...',
        }),
      )
    : /*#__PURE__*/ React.createElement(
        'div',
        {
          className: 'szzj-ding-qrcode',
        },
        /*#__PURE__*/ React.createElement(
          'div',
          {
            className: 'szzj-ding-qrcode-iframe',
            style: iframeStyle,
          },
          /*#__PURE__*/ React.createElement('iframe', {
            src: iframeSrc,
            style: iframeStyle,
          }),
        ),
        !pure &&
          /*#__PURE__*/ React.createElement(
            'div',
            {
              className: 'szzj-ding-qrcode-tips',
            },
            /*#__PURE__*/ React.createElement(
              'div',
              {
                style: {
                  display: 'flex',
                },
              },
              /*#__PURE__*/ React.createElement(
                'div',
                {
                  className: 'szzj-ding-qrcode-tips-icon',
                },
                /*#__PURE__*/ React.createElement(ScanOutlined, {
                  style: {
                    fontSize: '40px',
                  },
                }),
              ),
              /*#__PURE__*/ React.createElement(
                'div',
                {
                  className: 'szzj-ding-qrcode-tips-note',
                },
                '\u6253\u5F00',
                /*#__PURE__*/ React.createElement('span', null, '\u9489\u9489 APP'),
                /*#__PURE__*/ React.createElement('br', null),
                '\u626B\u7801\u767B\u5F55',
              ),
            ),
          ),
      );
}
